package com.j.lemon.learn;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lijunjun
 * @Date 2020/9/4 10:47
 * @Description
 */
public class TTTAAA {
    public static void main(String[] args) {
        try {
            Map<String,String> map = new HashMap<>();
            String logs = "optTime=20200904130850,sessionId=0000,sessionIdPa=0000,pracct=adminisator,slacct=linkagedx,srcIP=192.168.1.106,srcMAC=,srcHost=,dstIP=134.175.110.6,dstPort=1521,dstName=,dstId=,dbInstance=lcfa,dstHost=,oper=BEGIN,operProtocol=PLSQL,result=1,despCmd=BEGIN :ID := SYS.DBMS_TRANSACTION.LOCAL_TRANSACTION_ID; END; ,extJson=eyJjYXNTZXNzaW9uSWQiOiIifQ==,errMsg=,workOrder=,workOrderType=,fromSys=DB";
            String regex = "<\\d+>graphGate:optTime=(.*?),sessionId=(.*?),sessionIdPa=(.*?),pracct=(.*?),slacct=(.*?),srcIP=(.*?),srcMAC=(.*?),srcHost=(.*?),dstIP=(.*?),dstPort=(.*?),dstName=(.*?),dstId=(.*?),dbInstance=(.*?),dstHost=(.*?),oper=(.*?),operProtocol=(.*?),result=(.*?),despCmd=(.*?),errMsg=(.*?),workOrder=(.*?),workOrderType=(.*?),fromSys=(.*)";
            regex=regex.substring(regex.indexOf(":")+1);
            String[] split1 = regex.split(",");
            for (int i = 0; i < split1.length; i++) {
                split1[i] = split1[i].substring(0, split1[i].indexOf('=')+1);
            }
            for (int j = 0; j < split1.length-1; j++) {
                String s = StringUtils.substringBetween(logs,split1[j], split1[j + 1]);
                s = StringUtils.removeEnd(s,",");

                if(s!=null && s.contains("extJson=")){
                    map.put(split1[j].substring(0,split1[j].length()-1),s.substring(0,s.indexOf("extJson=")-1));
                    String extJsonValue = s.substring(s.indexOf("extJson=") + "extJson=".length());
                    if(!extJsonValue.startsWith("{")) {
                        byte[] bytes = Base64.decodeBase64(extJsonValue);
                        extJsonValue = new String(bytes);
                    }
                    map.put("extJson",extJsonValue);
                }else{
                    map.put(split1[j].substring(0,split1[j].length()-1),s);
                }
            }
            System.out.println(map);

//            String[] split = regex.split("\\(\\.\\*\\?\\)");
//            for (String sou : split) {
//                System.out.println(sou);
//            }
//            System.out.println(regex);
//            String s = StringUtils.substringBetween(logs, "optTime=", "sessionId=");
//            System.out.println(s);
//            String[] strings = StringUtils.split(logs,",",23);
//            for (String string : strings) {
//                System.out.println(string);
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
